package com.ali.order.message;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 接受MQ消息
 * @author Alistair.Chow
 */
@Component
public class MqReceiver {
    private static Logger logger = LoggerFactory.getLogger(MqReceiver.class);
    // 1. 制定队列 @RabbitListener(queues = "myQueue")
    // 2. 自动创建队列 @RabbitListener(queuesToDeclare = @Queue("myQueue"))
    // 3. 自动创建队列并与Exchange绑定
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("myQueue"),
            exchange = @Exchange("myExchange")
    ))
    public void process(String message){
        logger.info(String.format("MqReceiver: %s", message));
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("myQueue"),
            exchange = @Exchange("myExchange")
    ))

    /**
     *
     * 电脑供应商
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "computer",
            value = @Queue("computerQueue")
    ))
    public void processComputer(String message){
        logger.info(String.format("Computer MqReceiver: %s", message));
    }

    /**
     *
     * 水果供应商
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "fruit",
            value = @Queue("fruitQueue")

    ))
    public void processFruit(String message){
        logger.info(String.format("Fruit MqReceiver: %s", message));
    }
}